package servlet.man;

import bean.Hou;
import bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.HouInfoMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 李夏
 * @version 1.0
 */
//按照宿管id或者宿管名字模糊查询
@WebServlet("/man/getHouIdName")
public class GetHouIdNameServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Integer houId = null;
        if (Integer.valueOf(request.getParameter("houId")) != 0) {
            houId = Integer.valueOf(request.getParameter("houId"));

        }
        String houName = request.getParameter("houName");
        List<Hou> hous = null;
        try {
            hous = HouInfoMapper.getHouIdName(houId, houName);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        Result result = new Result();
        if (hous != null) {
            result.success(hous);
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        } else {
            result.fail();
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }
    }
}
